function [act,run_act,layer,allROIact,animalIDtxt,regionIDtxt,adata]=check_hom_data(animalID,regionID,vis_output,calc_output)

% num_hom(:,1) = animalID
% num_hom(:,2) = timepoint (-48,...,48)
% num_hom(:,3) = animal DB exp ID
% num_hom(:,4) = ExpID
% num_hom(:,8) = StackID (=ExpID)
% num_hom(:,9) = regionID
adata_dir='\\argon.fmi.ch\keller.g\AData\';
tmp_data_dir='D:\RawData\';
[num_hom,txt_hom]=xlsread('\\argon.fmi.ch\keller.g\AData\kellgeor\HOM\EnuExps.xlsx');


if nargin<4
    calc_output=0;
end

curr_ind=find(num_hom(:,1)==animalID&num_hom(:,9)==regionID);
if length(curr_ind)<4
    disp('Region does not exist, or too few data points were acquired - Aborting.')
    act=NaN;
    run_act=NaN;
    layer=NaN;
    allROIact=NaN;
    act_code=NaN;
    animalIDtxt=NaN;
    regionIDtxt=NaN;
    adata=NaN;
    return
end
ExpIDs=num_hom(curr_ind,4);
tpointIDs=num_hom(curr_ind,2);
animalIDtxt=txt_hom{min(find(num_hom(:,1)==animalID))+1,1};
regionIDtxt=txt_hom{min(curr_ind),11};
auxIDs=txt_hom(curr_ind+1,8);
layer=num_hom(min(curr_ind),11);

tpoints=[-48 -24 6 18 24 48 72];

disp('-----------------')
disp(sprintf('Animal %s (%i) region %i (%s)',animalIDtxt,animalID,regionID,regionIDtxt));
disp('-----------------')

for ind=1:length(ExpIDs)
    [adata_file,mouse_id,userID]=find_adata_file(ExpIDs(ind),adata_dir);
    adata(ind)=load([adata_dir userID '\' mouse_id '\' adata_file],'ROIs','template','act_map','fnames');
    disp(sprintf('t: %i \t expID: %i \t # ROIs: %i %i %i %i - %i',num_hom(curr_ind(ind),2),ExpIDs(ind),length(adata(ind).ROIs{1}),length(adata(ind).ROIs{2}),length(adata(ind).ROIs{3}),length(adata(ind).ROIs{4}),isfield(adata(ind).ROIs,'activity')))
    
    if calc_output
        aux_data=[];
        for knd=1:length(adata(ind).fnames)
            curr_load_path=[tmp_data_dir userID '\' mouse_id '\' adata(ind).fnames{knd}(1:end-10) '.lvd'];
            tmp_aux_data=load_lvd(curr_load_path);
            aux_data(1:9,end+1:end+size(tmp_aux_data,2))=tmp_aux_data;
        end

        run_act_tmp(ind)=sum(abs(diff(aux_data(4,:))));
        frame_times=get_frame_times(aux_data(2,:));
        tmp(ind).act_code=get_act_code(aux_data,frame_times);
    end
    %lp_set_str=inifile([tmp_data_dir mouse_id '\ImagingData\' date_id '\' adata(ind).fnames{1}(1:end-4) '.ini'],'read',{'main','','laser.eom.set'});
    %lp_set(ind)=str2num(lp_set_str{1}(2:end-1));
    %     a=load([adata_dir mouse_id '\' date_id '\' adata_file],'ROItrans');
    %     a.ROItrans
    %plot_all_ROIs_activity(adata(ind).ROIs);
end

if calc_output
    for ind=1:length(adata)
        adata(ind).act_code=int8(tmp(ind).act_code);
        %adata(ind).laser_power=lp_tmp(ind);
        %adata(ind).laser_power_set=lp_set(ind);
    end
end


if vis_output
    
    for jnd=1:4
        
        figure;
        set(gcf,'menubar','none')
        set(gcf,'color','k')
        colormap gray
        cnt=0;
        
        for knd=2:-1:1
            for ind=1:4
                cnt=cnt+1;
                try
                    axes('position',[1/4*(ind-1) 1/2*(knd-1) 1/4 1/2],'color','k');
                    imagesc(adata(cnt).template{jnd});
                    if cnt==1
                        text(.1,.125,'Template','units','normalized','color','w','fontweight','bold','Interpreter','none')
                        text(.1,.05,[animalIDtxt ' ' num2str(jnd)],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                    end
                    text(.1,.95,[num2str(tpointIDs(cnt)) 'h'],'units','normalized','color','w','fontweight','bold')
                    axis off
                end
            end
        end
        
        
        figure;
        set(gcf,'menubar','none')
        set(gcf,'color','k')
        colormap gray
        cnt=0;
        
        for knd=2:-1:1
            for ind=1:4
                try
                    cnt=cnt+1;
                    axes('position',[1/4*(ind-1) 1/2*(knd-1) 1/4 1/2],'color','k');
                    imagesc(adata(cnt).act_map{jnd});
                    if cnt==1
                        text(.1,.125,['Activity Map'],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                        text(.1,.05,[animalIDtxt ' ' num2str(jnd)],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                    end
                    text(.1,.85,[num2str(tpointIDs(cnt)) 'h'],'units','normalized','color','w','fontweight','bold')
                    axis off
                end
            end
        end
        
        figure;
        set(gcf,'menubar','none')
        set(gcf,'color','k')
        cnt=0;
        cntall=0;
        tmpall=zeros(size(adata(1).act_map{1}));
        for knd=2:-1:1
            for ind=1:4
                try
                    cnt=cnt+1;
                    axes('position',[1/4*(ind-1) 1/2*(knd-1) 1/4 1/2],'color','k');
                    
                    tmp=zeros(size(adata(cnt).act_map{jnd}));
                    
                    for lnd=1:length(adata(cnt).ROIs{jnd})
                        cntall=cntall+1;
                        tmp(adata(cnt).ROIs{jnd}(lnd).indices)=lnd;
                        tmpall(adata(cnt).ROIs{jnd}(lnd).indices)=cnt;
                    end
                    
                    imagesc(tmp);
                    
                    
                    if cnt==1
                        text(.1,.125,['ROIs'],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                        text(.1,.05,[animalIDtxt ' ' num2str(jnd)],'units','normalized','color','w','fontweight','bold','Interpreter','none')
                    end
                    text(.1,.85,[num2str(tpointIDs(cnt)) 'h'],'units','normalized','color','w','fontweight','bold')
                    axis off
                end
            end
        end
        figure;
        set(gcf,'menubar','none')
        axes('position',[0 0 1 1],'color','k');
        imagesc(tmpall)
        
    end
end



if calc_output
    for knd=1:4
        for ind=1:length(adata)
            curr_ind=find(tpointIDs(ind)==tpoints);
            run_act(curr_ind)=run_act_tmp(ind);
            
            mean_act{knd}(curr_ind)=mean(adata(ind).act_map{knd}(:));
            mean_template{knd}(curr_ind)=mean(adata(ind).template{knd}(:));
            
            tmp=adata(ind).act_map{knd}.*(adata(ind).template{knd}+50)./adata(ind).template{knd};
            bright_pixs=(adata(ind).template{knd}>prctile(adata(ind).template{knd}(:),25));
            tmp=double(tmp).*double(bright_pixs);
            tmp=imerode(tmp>3,ones(3));
            act{knd}(curr_ind)=sum(tmp(:));
            %allROIact=NaN;
            
            sig_act{knd}=zeros(length(adata(ind).ROIs{knd}(1).activity),length(adata(ind).ROIs{knd}));
            r_act_mat{knd}=zeros(length(adata(ind).ROIs{knd}(1).activity),length(adata(ind).ROIs{knd}));
            if isfield(adata(ind).ROIs{knd},'activity');
                for jnd=1:length(adata(ind).ROIs{knd})
                    r_act_mat{knd}(1:length(adata(ind).ROIs{knd}(jnd).activity),jnd)=adata(ind).ROIs{knd}(jnd).activity;
                end
                for jnd=1:length(adata(ind).ROIs{knd})
                    tmp=find_active_ind(r_act_mat{knd}(:,jnd),4,mean(adata(ind).template{knd}(:))/4);
                    %tmp(7000:10000)=0;
                    allROIact{knd}(curr_ind,jnd)=sum(tmp);
                    sig_act{knd}(:,jnd)=tmp;
                end
            end
            %adata(ind).sig_act=logical(sig_act);
            adata(ind).sig_act{knd}=sig_act{knd};
            adata(ind).timepoint{knd}=tpointIDs(ind);
            adata(ind).act_map{knd}=int16(32*adata(ind).act_map{knd});
            adata(ind).template{knd}=int16(32*adata(ind).template{knd});
            %         for knd=1:length(adata(ind).ROIs)
            %             adata(ind).ROItype(knd)=double(adata(ind).ROIs(knd).type);
            %         end
        end
    end
    adata=rmfield(adata,'ROIs');
else
    allROIact=NaN;
    act=NaN;
    run_act=NaN;
    act_code=NaN;
end


% figure
% for ind=1:length(adata(1).ROIs)
%     tmp=zeros(size(adata(1).template));
%     for knd=1:length(adata)
%         tmp(adata(knd).ROIs(ind).indices)=knd;
%     end
%     imagesc(tmp);
%     drawnow
%     pause
% end
















